<template>
  <div>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="菜品编码" prop="dishCode">-->
<!--      <el-input v-model="dataForm.dishCode" placeholder="菜品编码"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="菜品名称" prop="dishName">
      <el-input v-model="dataForm.dishName" placeholder="菜品名称"></el-input>
    </el-form-item>
    <el-form-item label="所需物料" prop="reqMate">
      <el-input v-model="dataForm.reqMate" placeholder="所需物料"></el-input>
    </el-form-item>
    <el-form-item label="菜品类型(0-普通,1-今日推荐,2-特色菜,其他为套餐码)" prop="dishType">
      <el-input v-model="dataForm.dishType" placeholder="菜品类型(0-普通,1-今日推荐,2-特色菜,其他为套餐码)"></el-input>
    </el-form-item>
    <el-form-item label="菜品价格" prop="dishPrice">
      <el-input v-model="dataForm.dishPrice" placeholder="菜品价格"></el-input>
    </el-form-item>
    <el-form-item label="健康标签" prop="label">
        <el-button type="primary" @click="showDialog">添加健康信息</el-button>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>


  <el-dialog title="添加数据" :visible.sync="dialogVisible">
    <el-form :model="formData" label-width="80px">
      <el-form-item label="菜品名称">
        <el-input v-model="dataForm.dishName" :disabled="true" ></el-input>
      </el-form-item>
      <el-form-item label="能量">
        <el-input v-model="formData.energy"></el-input>
      </el-form-item>
      <el-form-item label="蛋白质">
        <el-input v-model="formData.protein"></el-input>
      </el-form-item>
      <el-form-item label="碳水化合物">
        <el-input v-model="formData.carbohydrate"></el-input>
      </el-form-item>
      <el-form-item label="脂肪">
        <el-input v-model="formData.fat"></el-input>
      </el-form-item>
      <el-form-item label="糖">
        <el-input v-model="formData.sugar"></el-input>
      </el-form-item>
      <el-form-item label="饱和脂肪">
        <el-input v-model="formData.saturatedFat"></el-input>
      </el-form-item>
      <el-form-item label="反式脂肪">
        <el-input v-model="formData.transFat"></el-input>
      </el-form-item>
      <el-form-item label="胆固醇">
        <el-input v-model="formData.cholesterol"></el-input>
      </el-form-item>

      <el-form-item label="钠">
        <el-input v-model="formData.sodium"></el-input>
      </el-form-item>

    </el-form>
    <div slot="footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="saveData">保存</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'

  export default {
    data () {
      return {

        dialogVisible: false,
        formData: {

          dishCode: '',
          energy: '',
          protein: '',
          carbohydrate: '',
          fat: '',
          sugar: '',
          saturatedFat: '',
          transFat: '',
          cholesterol: '',
          sodium: ''
        },
        visible: false,
        dataForm: {
          id: 0,
          dishCode: '',
          dishName: '',
          reqMate: '',
          dishType: '',
          dishPrice: ''
          // label: ''
        },
        dataRule: {
          // dishCode: [
          //   { required: true, message: '菜品编码不能为空', trigger: 'blur' }
          // ],
          dishName: [
            { required: true, message: '菜品名称不能为空', trigger: 'blur' }
          ],
          reqMate: [
            { required: true, message: '所需物料不能为空', trigger: 'blur' }
          ],
          dishType: [
            { required: true, message: '菜品类型(0-普通,1-今日推荐,2-特色菜,其他为套餐码)不能为空', trigger: 'blur' }
          ],
          dishPrice: [
            { required: true, message: '菜品价格不能为空', trigger: 'blur' }
          ]
          // label: [
          //   { required: true, message: '健康标签不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      showDialog () {
        this.dialogVisible = true
      },
      saveData () {
        this.dialogVisible = false
        // // 发送 AJAX 请求添加数据
        // axios.post('http://localhost:8081/menu/nutrienttable/save')
        //   .then(response => {
        //     // 添加成功后关闭弹出框，并提示用户
        //     if (response.data.code === 0) {
        //       this.dialogVisible = false
        //       this.$message.success(response.data.message)
        //     }
        //   })
        //   .catch(error => {
        //     // 处理异常情况
        //     console.log(error)
        //   })
      },
      init (id) {
        alert(111)
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `http://localhost:8081/menu/menu/info/${this.dataForm.id}`,
              // url: this.$http.adornUrl(`/menu/menu/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dishCode = data.menu.dishCode
                this.dataForm.dishName = data.menu.dishName
                this.dataForm.reqMate = data.menu.reqMate
                this.dataForm.dishType = data.menu.dishType
                this.dataForm.dishPrice = data.menu.dishPrice
                // this.dataForm.label = data.menu.label
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              // url: this.$http.adornUrl(`/menu/menu/${!this.dataForm.id ? 'save' : 'update'}`),
              url: `http://localhost:8081/menu/menu/${!this.dataForm.id ? 'save' : 'update'}`,

              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'dishCode': this.dataForm.dishCode,
                'dishName': this.dataForm.dishName,
                'reqMate': this.dataForm.reqMate,
                'dishType': this.dataForm.dishType,
                'dishPrice': this.dataForm.dishPrice,
                'formData': this.formData
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
